/**  
 * 文 件 名: RightAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.system.rightsMgmt.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.rightsMgmt.dao.IRightDAO;
import com.tongzhou.system.rightsMgmt.model.Right;
import com.tongzhou.system.rightsMgmt.model.SearchRightCondition;
import com.tongzhou.util.Util;

public class RightAction extends ActionSupport{
	private String id;
	private String name;//权限名称
	private String url;//权限对应的URL
	private String module_id;//所属模块ID
	private String active;//是否可用(Y：可用;N：不可用)
	private String parent_id;//父权限id
	
	private String parent_name;//父权限名称
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private SearchRightCondition condition;
	private IRightDAO dao;
	private Right right;
	private List<Right> result;
	private List<Right> rightList1;
	private List<Right> rightList2;
	private List<Right> rightList3;
	private String judgment;
	private String subRightList;
	
	private String parent_id1;
	private String parent_id2;
	private String parent_id3;
	
	public String listRight() throws DBException{
		checkSearchCondition();
		condition = new SearchRightCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setName(name);
		condition.setActive(active);
		condition.setParent_name(parent_name);
		condition.setCurrentPage(currentPage);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType);
		condition.setPageSize(pageSize);
	    result = dao.listRight(condition);
		return "listRight";
	}
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "r.parent_id asc,r.id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "asc" ;
		}
	}
	
	public String toAddRight() throws DBException{
		right=new Right();
		right.setParent_id("0");
		rightList1=dao.getRightListByParentID(right);
		return "toAddRight";
	}
	
	public String checkRightByName() throws DBException{
		right=new Right();
		try {
			right.setName(new String(name.getBytes("ISO-8859-1"),"UTF-8"));
			right.setId(id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkRightByName(right);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	public String addRight() throws DBException{
		right=new Right();
		right.setName(name);
		right.setUrl(url);
		right.setParent_id(parent_id);
		right.setModule_id(module_id);
		right.setActive(active);
		dao.addRight(right);
		listRight();
		return "listRight";
	}
	
	public String getRightListByParentID() throws DBException{
		right=new Right();
		right.setParent_id(parent_id);
		result=dao.getRightListByParentID(right);
		
		subRightList="";
		for(Right m:result){
			if(!subRightList.equals("")){
				subRightList+=";";
			}
			subRightList+=m.getId()+":"+m.getName();
		}
		return SUCCESS;
	}
	
	public String deleteRight() throws DBException{
		right=new Right();
		right.setId(id);
		
		dao.deleteRight(right);
		listRight();
		name="";
		return "listRight";
	}
	
	public String toEditRight() throws DBException{
		//获取一级菜单
		right=new Right();
		right.setId(id);
		right=dao.getRightById(right);
		Right r=new Right();
		r.setParent_id("0");
		rightList1=dao.getRightListByParentID(r);
		
		int i=0;
		List<String> l=new ArrayList<String>();
		if(!Util.checkObjIsNull(right.getParent_id())&&!right.getParent_id().equals("0")){
			String pid=right.getParent_id();
			while(!pid.equals("0")){
				l.add(pid);
				i++;
				r.setId(pid);
				Right r2=dao.getRightById(r);
				pid=r2.getParent_id();
			}
		}
		if(i==1){
			parent_id1=right.getParent_id();
		}
		if(i==2){
			r.setParent_id(l.get(1));
			rightList2=dao.getRightListByParentID(r);
			parent_id1=l.get(1);
			parent_id2=l.get(0);
		}else if(i==3){
			r.setParent_id(l.get(1));
			rightList3=dao.getRightListByParentID(r);
			
			r.setParent_id(l.get(2));
			rightList2=dao.getRightListByParentID(r);
			
			parent_id1=l.get(2);
			parent_id2=l.get(1);
			parent_id3=l.get(0);
		}
		return "toEditRight";
	}
	
	public String editRight() throws DBException{
		right=new Right();
		right.setId(id);
		right.setName(name);
		right.setActive(active);
		right.setModule_id(module_id);
		right.setParent_id(parent_id);
		right.setUrl(url);
		dao.updateRight(right);
		listRight();
		return "listRight";
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getModule_id() {
		return module_id;
	}
	public void setModule_id(String moduleId) {
		module_id = moduleId;
	}
	public String getActive() {
		return active;
	}
	public void setActive(String active) {
		this.active = active;
	}
	public String getParent_id() {
		return parent_id;
	}
	public void setParent_id(String parentId) {
		parent_id = parentId;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public String getOrderByType() {
		return orderByType;
	}
	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}
	public SearchRightCondition getCondition() {
		return condition;
	}
	public void setCondition(SearchRightCondition condition) {
		this.condition = condition;
	}
	public IRightDAO getDao() {
		return dao;
	}
	public void setDao(IRightDAO dao) {
		this.dao = dao;
	}
	public Right getRight() {
		return right;
	}
	public void setRight(Right right) {
		this.right = right;
	}
	public List<Right> getResult() {
		return result;
	}
	public void setResult(List<Right> result) {
		this.result = result;
	}
	public String getParent_name() {
		return parent_name;
	}
	public void setParent_name(String parentName) {
		parent_name = parentName;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getJudgment() {
		return judgment;
	}
	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}
	public String getSubRightList() {
		return subRightList;
	}
	public void setSubRightList(String subRightList) {
		this.subRightList = subRightList;
	}
	public List<Right> getRightList1() {
		return rightList1;
	}
	public void setRightList1(List<Right> rightList1) {
		this.rightList1 = rightList1;
	}
	public List<Right> getRightList2() {
		return rightList2;
	}
	public void setRightList2(List<Right> rightList2) {
		this.rightList2 = rightList2;
	}
	public List<Right> getRightList3() {
		return rightList3;
	}
	public void setRightList3(List<Right> rightList3) {
		this.rightList3 = rightList3;
	}
	public String getParent_id1() {
		return parent_id1;
	}
	public void setParent_id1(String parentId1) {
		parent_id1 = parentId1;
	}
	public String getParent_id2() {
		return parent_id2;
	}
	public void setParent_id2(String parentId2) {
		parent_id2 = parentId2;
	}
	public String getParent_id3() {
		return parent_id3;
	}
	public void setParent_id3(String parentId3) {
		parent_id3 = parentId3;
	}
}
